##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Figure 8: Lowess Plot of Effect Heterogeneity by Priors

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################

library(MASS)
library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(scales)

##################################################################

rm(list=ls())

data.working <- readRDS("d_r_cleaned_analysis_dataset.Rda")

myvars <- c("q8", "q8.toolittle", "q11.yes", "q11", "q11.yes.nodk", "q11.dk", "q13ab.3", "q11.no", "q13ab.3.positive","rank.estimate.diff", "rank.estimate.diff.bias", "median.estimate.topcoded", "group3","group2")
data.subset <- data.working[myvars]
data.subset <- data.subset[data.subset$group3==1| data.subset$group2==1,]

data.subset$group3 <- factor(data.subset$group3)
median.aid <- ggplot(data.subset, aes(median.estimate.topcoded, q8.toolittle, group=group3))
median.aid <- median.aid + stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(0, 10000, 20000, 30000, 40000), limits=c(0, 50000)) + xlab("Estimate of Global Median Income ($)") +
ylab("Increase Foreign Aid (1/0)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue"), breaks=c(0, 1), labels=c("No Information", "Information")) +
theme(legend.position="none") 
median.aid

median.giving <- ggplot(data.subset, aes(median.estimate.topcoded, q13ab.3, group=group3))
median.giving <- median.giving + stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(0, 10000, 20000, 30000, 40000), limits=c(0, 50000)) + xlab("Estimate of Global Median Income ($)") +
ylab("Donation to Int. Charity (%)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue"), breaks=c(0, 1), labels=c("No Information", "Information")) +
theme(legend.position="none") 
median.giving

median.anygiving <- ggplot(data.subset, aes(median.estimate.topcoded, q13ab.3.positive, group=group3))
median.anygiving <- median.anygiving + stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(0, 10000, 20000, 30000, 40000), limits=c(0, 50000)) + xlab("Estimate of Global Median Income ($)") +
ylab("Any Donation to Int. Charity (1/0)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue"), breaks=c(0, 1), labels=c("No Information", "Information")) +
theme(legend.position="none") 
median.anygiving

median.agro <- ggplot(data.subset, aes(median.estimate.topcoded, q11.yes.nodk, group=group3))
median.agro <- median.agro + stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(0, 10000, 20000, 30000, 40000), limits=c(0, 50000)) + xlab("Estimate of Global Median Income ($)") +
ylab("Reduce Ag. Trade Protections (1/0)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue"), breaks=c(0, 1), labels=c("No Information", "Information")) +
theme(legend.position="none") 
median.agro

rank.aid <- ggplot(data.subset, aes(rank.estimate.diff.bias, q8.toolittle, group=group3))
rank.aid<- rank.aid+ stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(-20, 0, 20, 40, 60), limits=c(-20, 60)) + xlab("Respondent's Estimate of Rank - True Rank") +
ylab("Increase Foreign Aid (1/0)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue")) +
theme(legend.position="none") 
rank.aid

rank.giving<- ggplot(data.subset, aes(rank.estimate.diff.bias, q13ab.3, group=group3))
rank.giving<- rank.giving+ stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(-20, 0, 20, 40, 60), limits=c(-20, 60)) + xlab("Respondent's Estimate of Rank - True Rank") +
ylab("Donation to Int. Charity (%)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue")) +
theme(legend.position="none") 
rank.giving

rank.anygiving<- ggplot(data.subset, aes(rank.estimate.diff.bias, q13ab.3.positive, group=group3))
rank.anygiving<- rank.anygiving+ stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(-20, 0, 20, 40, 60), limits=c(-20, 60)) + xlab("Respondent's Estimate of Rank - True Rank") +
ylab("Any Donation to Int. Charity (1/0)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue")) +
theme(legend.position="none") 
rank.anygiving

rank.agro<- ggplot(data.subset, aes(rank.estimate.diff.bias, q11.yes, group=group3))
rank.agro<- rank.agro+ stat_smooth(level=0.90, fill=c("gray80")) + 
scale_x_continuous(breaks=c(-20, 0, 20, 40,60), limits=c(-20, 60)) + xlab("Respondent's Estimate of Rank - True Rank") +
ylab("Reduce Ag. Trade Protections (1/0)")  +
theme_bw() +
theme(axis.text=element_text(size=6), axis.title=element_text(size=6), plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm")) +
aes(colour = factor(group3)) +
scale_colour_manual(values=c("red", "navyblue")) +
theme(legend.position="none") 
rank.agro

 multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}
ggsave(filename="tf_f_8_1_lowess_het_priors_median_giving.png", plot=median.giving, width=3, height=2)
ggsave(filename="tf_f_8_2_lowess_het_priors_rank_giving.png", plot=rank.giving, width=3, height=2)
ggsave(filename="tf_f_8_3_lowess_het_priors_median_aid.png", plot=median.aid, width=3, height=2)
ggsave(filename="tf_f_8_4_lowess_het_priors_rank_aid.png", plot=rank.aid, width=3, height=2)
ggsave(filename="tf_f_8_5_lowess_het_priors_median_agro.png", plot=median.agro, width=3, height=2)
ggsave(filename="tf_f_8_6_lowess_het_priors_rank_agro.png", plot=rank.agro, width=3, height=2)

multiplot(median.giving, median.aid, median.agro,  rank.giving, rank.aid,  rank.agro, cols=2)

